#include <OneWire.h>
#include "SegDisplay.h"
#include "Timer.h"

OneWire ds(10);

float raw = 0;
Timer t;
int led = 13;
void setup(){
    Serial.begin(9600);
    pinMode(led, OUTPUT);
    pinMode(12,OUTPUT);
    pinMode(11,OUTPUT);
    
    t.every(1000, printNhietDo);
    t.every(2000, ReadTemp);
}
void loop(){
  
   t.update();
}
void ReadTemp(){
  byte addr[8];
  byte data[12];
   if(!ds.search(addr)){
       if(OneWire::crc8(addr,7) == addr[7]){
           ds.reset();
           ds.select(addr);
           ds.write(0x44,1);
           delay(1000);
           ds.reset();
           ds.select(addr);    
           ds.write(0xBE);
           for(unsigned i=0;i<9;i++){
               data[i] = ds.read();
           }
           raw = (data[1] << 8) | data[0];
           
           raw = raw / 16.0;
           
       }  
   }
}



void printNhietDo()
{
  Serial.println(raw);
  if(raw >= 24){
    digitalWrite(led, HIGH);
    digitalWrite(11,LOW);
  }
  if(raw <=22){
    digitalWrite(led, LOW);
    digitalWrite(11,HIGH);
  }
  

}
